package com.turing.post.antimoney.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;

import com.turing.post.antimoney.util.AlarmInfo;
import com.turing.post.antimoney.util.AlarmInfoDao;
import com.turing.post.antimoney.util.AlarmInfoDetail;
import com.turing.post.antimoney.util.AlarmInfoDetailDao;
import com.turing.post.antimoney.util.AlarmRule;
import com.turing.post.antimoney.util.AlarmRulesBusiness;
import com.turing.post.capitalaccounts.util.BankAccount;
import com.turing.post.capitalaccounts.util.BankAccountDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.organization.util.IssueCompany;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.organization.util.SaleBranch;
import com.turing.post.organization.util.SaleBranchDao;
import com.turing.post.salesmanage.util.PersonalCustom;
import com.turing.post.salesmanage.util.PersonalCustomDao;
import com.turing.post.salesmanage.util.SaleOrder;
import com.turing.post.salesmanage.util.SaleOrderDao;
import com.turing.post.util.PCPSApplicationException;

/**
 *  同一购卡人使用转账方式购卡，转账账户名称与购卡人名称不符的
 * 
 * @author 李治
 * 
 */
@Remote
@Local
@Stateless(mappedName = "antimoney.impl.AlarmRule5")
public class AlarmRule5 implements AlarmRulesBusiness {

	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao issueCompanyDaoImpl;
	@EJB(mappedName = "salesmanage.impl.SaleOrderDaoImpl")
	private SaleOrderDao saleOrderDaoImpl;
	@EJB(mappedName = "	salesmanage.impl.PersonalCustomDaoImpl")
	private PersonalCustomDao personalCustomDaoImpl;
	@EJB(mappedName = "	capitalaccounts.impl.BankAccountDaoImpl")
	private BankAccountDao bankAccountDaoImpl;
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;
	@EJB(mappedName = "organization.impl.SaleBranchDaoImpl")
	private SaleBranchDao saleBranchDaoImpl;
	@EJB(mappedName = "antimoney.impl.AlarmInfoDaoImpl")
	private AlarmInfoDao alarmInfoDaoImpl;
	@EJB(mappedName = "antimoney.impl.AlarmInfoDetailDaoImpl")
	private AlarmInfoDetailDao alarmInfoDetailDaoImpl;

	// 同一购卡人使用转账方式购卡，转账账户名称与购卡人名称不符的
	@Override
	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void runAlarmRules(String issueCompanyNo, Date tranDate,
			AlarmRule alarmRule) throws PCPSApplicationException {
		// 1.查询销售单是转账方式的
		List<Object[]> list = saleOrderDaoImpl.getSaleOrderForAlarm(tranDate,
				issueCompanyNo);
		List<SaleOrder> alarmOrders = new ArrayList<SaleOrder>();
		IssueCompany issueCompany = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(issueCompanyNo);
		SaleOrder alarmOrder = null;
		PersonalCustom person = null;
		BankAccount bankAcc = null;
		for (int i = 0; i < list.size(); i++) {
			person = personalCustomDaoImpl
					.getPersonalCustomByIdNo(list.get(i)[0].toString());
			bankAcc = bankAccountDaoImpl
					.getBankAccountByBankAccountId((BigInteger) list.get(i)[1]);
			// 2.找出够卡人和付款人信息不一致的(存在风险交易)
			if (!person.getName().equals(bankAcc.getAccountName())) {
				alarmOrder = saleOrderDaoImpl
						.getSaleOrderBySaleOrderId((BigInteger) list.get(i)[2]);
				alarmOrders.add(alarmOrder);
			}
		}
		releaseAlarm(alarmOrders, issueCompany, alarmRule, tranDate, person);// 发布风险信息
	}

	/**
	 * 发布风险信息
	 * 
	 * @param tranDate
	 * @param alarmRule
	 * @param issueCompany
	 * 
	 * @param alarmOrders存在风险的销售单
	 * @throws PCPSApplicationException
	 */
	private void releaseAlarm(List<SaleOrder> alarmOrders,
			IssueCompany issueCompany, AlarmRule alarmRule, Date tranDate,
			PersonalCustom person) throws PCPSApplicationException {
		AlarmInfo alarmInfo = new AlarmInfo();
		SaleOrder lastSaleOrder = alarmOrders.get(0);
		SaleBranch saleBranch = saleBranchDaoImpl
				.getSaleBranchBySaleBranchNo(lastSaleOrder.getSaleBranchNo());
		alarmInfo.setAlarmRuleId(alarmRule.getAlarmRuleId());
		alarmInfo.setIssueCompanyNo(issueCompany.getIssueCompanyNo());
		alarmInfo.setBranchNo(lastSaleOrder.getSaleBranchNo());
		alarmInfo.setBranchName(saleBranch.getSaleBranchName());
		alarmInfo.setAlarmTime(commonDaoImpl.getDatabaseSysTime());
		alarmInfo.setRiskCustomName(person.getName());
		alarmInfo.setAlarmReason(alarmRule.getAlarmRuleDesc());
		alarmInfo.setDealStatus("0");

		BigInteger alarmInfoId = alarmInfoDaoImpl.getMaxAlarmInfoId();
		alarmInfo.setAlarmInfoId(alarmInfoId);
		// 插入预警信息
		alarmInfoDaoImpl.insertAlarmInfo(alarmInfo);
		for (int i = 1; i <= alarmOrders.size(); i++) {
			SaleOrder order = alarmOrders.get(i - 1);
			AlarmInfoDetail alarmInfoDetail = new AlarmInfoDetail();
			alarmInfoDetail.setAlarmInfoId(alarmInfoId);
			alarmInfoDetail.setSeqNo(i);
			alarmInfoDetail.setBusinessType("0");
			alarmInfoDetail.setBusinessOrderId(order.getSaleOrderId());
			alarmInfoDetail.setTranDate(tranDate);
			alarmInfoDetailDaoImpl.insertAlarmInfoDetail(alarmInfoDetail);
		}
	}
}
